import math

from ship_envs.core.model import Model
from shapely.geometry import Polygon
from shapely import affinity


class PolygonModel(Model):

    def __init__(self, pts=None):
        super().__init__()
        if pts is None:
            pts = [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0)]
        self.gemo = Polygon(pts)

    # 获取几何模型的所有坐标点
    def get_gemo_pts(self):
        return self.gemo.exterior.coords[:]

    def get_scaled_pts(self, xfact, yfact):
        x, y = self.gemo.centroid.coords[0]
        moved_geom = affinity.translate(self.gemo, xfact * x - x, yfact * y - y)
        scaled_gemo = affinity.scale(moved_geom, xfact, yfact)
        return scaled_gemo.exterior.coords[:]

    def move(self, distance):
        self._move(math.cos(math.radians(self.angle)) * distance, math.sin(math.radians(self.angle)) * distance)

    def _move(self, xoff, yoff):
        if self.gemo:
            self.gemo = affinity.translate(self.gemo, xoff, yoff)

    def rotate(self, angle):
        self.angle += angle
        if self.gemo:
            self.gemo = affinity.rotate(self.gemo, angle, use_radians=False)

    def get_pos(self):
        return self.gemo.centroid.coords[0]

    def contains(self, other):
        if not other:
            # raise error?
            return False
        if self.gemo and other.gemo:
            return self.gemo.contains(other.gemo)
        return False

    def collision_with_other(self, other):
        if not other:
            # raise error?
            return False
        if self.gemo and other.gemo:
            return not self.gemo.disjoint(other.gemo)
        return False
